package com.chookss.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.MyApp;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.KnowledgeEntity;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.video.recommend.LongVediosActivity;
import com.chookss.view.FlowLayoutManager;
import com.chookss.view.MaxHeightRecyclerView;
import com.chookss.view.consecutivescroller.ConsecutiveScrollerLayout;
import com.google.gson.reflect.TypeToken;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//
public class VideoNewCatesSecondActivity extends BaseAct {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.topFlowLayout)
    RecyclerView titleRcv;
    @BindView(R.id.rcvDirectory)
    RecyclerView recyclerView;
    @BindView(R.id.tvBottom)
    TextView tvBottom;
    @BindView(R.id.tvBottomSure)
    TextView tvBottomSure;
    @BindView(R.id.llLine2)
    View llLine2;
    @BindView(R.id.llBottomSure)
    LinearLayout llBottomSure;
    @BindView(R.id.ivBottomArrow)
    ImageView ivBottomArrow;
    @BindView(R.id.llBottomArrow)
    LinearLayout llBottomArrow;
    @BindView(R.id.llBottomPopup)
    LinearLayout llBottomPopup;
    @BindView(R.id.llNone)
    LinearLayout llNone;
    @BindView(R.id.smrRf)
    SmartRefreshLayout smrRf;
    @BindView(R.id.folderFlowLayout)
    MaxHeightRecyclerView folderSelectLayout;
    @BindView(R.id.consecutiveScrollerLayout)
    ConsecutiveScrollerLayout consecutiveScrollerLayout;
    private boolean isOpen = false;//是否展开弹窗默认关闭
    private String videoType = "2";//1小视频2长视频 3发布或者修改视频所用
    private List<KnowledgeEntity> folderList = new ArrayList<>();
    private FolderAdapter adapter;
    private List<KnowledgeEntity> folderSelectList = new ArrayList<>();
    private FolderSelectAdapter folderSelectAdapter;
    private int level = 1;//默认层级
    //目录
    private TitleAdapter titleAdapter;
    private ArrayList<String> nameList = new ArrayList<>();//组织排列;
    private String parentCode = "", hierarchyOrgCode = "";//各个层级需要传的
    private boolean oldDepartmentStatus = false;//上一层级归属部门是否全选
    private int selectDirectoryAllNum = 0;//当前目录选中数量
    private int directoryAllNum = 0;//当前目录数量
    private int selectAllNum = 0;//整个视频目录选中文件总数
    private KnowledgeEntity oldDepartmentEntity = null;//上一层级归属部门实体类
    private KnowledgeEntity levelOne;//一级目录数据
    public static VideoNewCatesSecondActivity videoNewCatesSecondActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_video_cates_second);
        videoNewCatesSecondActivity = this;
        KLog.i("====" + "onCreate" + System.currentTimeMillis() + MyApp.activitys.size());
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        videoType = getIntent().getStringExtra("videoType");
        parentCode = getIntent().getStringExtra("parentCode");
        hierarchyOrgCode = getIntent().getStringExtra("hierarchyOrgCode");
        level = getIntent().getIntExtra("level", 1);
        if (level > 1) {
            videoNewCatesSecondActivity = null;
        }
        KLog.i("====" + "initData" + level + "=" + System.currentTimeMillis());
        oldDepartmentStatus = getIntent().getBooleanExtra("oldDepartmentStatus", false);
        oldDepartmentEntity = (KnowledgeEntity) getIntent().getSerializableExtra("oldDepartmentEntity");
        levelOne = (KnowledgeEntity) getIntent().getSerializableExtra("levelOne");
        ArrayList<String> nameLists = getIntent().getStringArrayListExtra("nameList");
        if (nameLists != null) {
            nameList.addAll(nameLists);
        }
        if (nameList.size() > 1) {
            tvTitle.setText(nameList.get(nameList.size() - 1));
            titleAdapter = new TitleAdapter(R.layout.item_folder_name_list, this, nameList);
            LinearLayoutManager lm = new LinearLayoutManager(this);
            lm.setOrientation(LinearLayoutManager.HORIZONTAL);
            titleRcv.setLayoutManager(lm);
            titleRcv.setAdapter(titleAdapter);
            titleRcv.setVisibility(View.VISIBLE);
            llLine2.setVisibility(View.VISIBLE);
        } else {
            if (oldDepartmentEntity != null) {
                tvTitle.setText(oldDepartmentEntity.getName());
            }

        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new FolderAdapter(R.layout.item_video_folder, this, folderList, nameList, level, videoType);
        recyclerView.setAdapter(adapter);
        folderSelectAdapter = new FolderSelectAdapter(R.layout.item_video_folder_select_list, this, folderSelectList);
        FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        folderSelectLayout.setLayoutManager(flowLayoutManager);
        folderSelectLayout.setAdapter(folderSelectAdapter);
        smrRf.setEnableLoadMore(false);
        smrRf.setEnableRefresh(false);
        getCatalogData();
    }

    private void getCatalogData() {
        Map<String, String> map = new HashMap<>();
        map.put("parentCode", parentCode);
        //        fileFlag 0不包含当前目录文件 1包含
        String url = "";
        if ("3".equals(videoType)) {
            map.put("fileFlag", "0");
            map.put("totalFlag", "0");
            map.put("catalogType", "1");
            url = Urls.selectAPPCatalogTreeGlobal;
        } else if ("5".equals(videoType) || "6".equals(videoType)) {
            map.put("fileFlag", "1");
            map.put("totalFlag", "0");
            url = Urls.getAppImageCatalogTree;
        } else {
            map.put("fileFlag", "1");
            map.put("totalFlag", "1");
            map.put("catalogType", videoType);
            url = Urls.selectAPPCatalogTreeGlobal;
        }
        showLoading();
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<List<KnowledgeEntity>>>() {
        }.getType(), null, url, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<KnowledgeEntity>>() {
            @Override
            public void onSuccess(List<KnowledgeEntity> data) {
                dismissLoading();
                if (data != null && data.size() > 0) {
                    folderList.clear();
                    folderList.addAll(data);
                    for (int i = 0; i < folderList.size(); i++) {
                        folderList.get(i).setHierarchyOrgCode(hierarchyOrgCode);
                        if (oldDepartmentEntity != null) {
                            folderList.get(i).setFolderName(oldDepartmentEntity.getName());
                        }
                        if (oldDepartmentStatus) {
                            folderList.get(i).setSelect(true);
                            folderList.get(i).setHalfSelect(false);
                            folderList.get(i).setUpperHalfSelect(false);
                            folderList.get(i).setHalfSelectNum(folderList.get(i).getFileNum());
                            selectDirectoryAllNum++;
                            for (int j = 0; j < MyApp.videoFolderList.size(); j++) {
                                if (folderList.get(i).getCode().equals(MyApp.videoFolderList.get(j).getCode())) {
                                    MyApp.videoFolderList.remove(j);
                                    if (j < MyApp.videoFolderList.size()) {
                                        j--;
                                    }
                                } else if (MyApp.videoFolderList.get(j).getHierarchyOrgCode().contains(folderList.get(i).getCode())) {
                                    MyApp.videoFolderList.remove(j);
                                    if (j < MyApp.videoFolderList.size()) {
                                        j--;
                                    }
                                }
                            }
                            MyApp.videoFolderList.add(folderList.get(i));
                            if (i == folderList.size() - 1) {
                                changeSelectStatus();
                            }
                        } else {
                            for (int j = 0; j < MyApp.videoFolderList.size(); j++) {
                                if (folderList.get(i).getCode().equals(MyApp.videoFolderList.get(j).getCode())) {
                                    MyApp.videoFolderList.get(j).setUpperHalfSelect(false);
                                    folderList.get(i).setSelect(MyApp.videoFolderList.get(j).isSelect());
                                    folderList.get(i).setHalfSelect(MyApp.videoFolderList.get(j).isHalfSelect());
                                    folderList.get(i).setUpperHalfSelect(false);
                                    folderList.get(i).setHalfSelectNum(MyApp.videoFolderList.get(j).getHalfSelectNum());
                                }
                                if (i == folderList.size() - 1 && j == MyApp.videoFolderList.size() - 1) {
                                    changeSelectStatus();
                                }
                            }
                        }
                    }
                    directoryAllNum = folderList.size();
                    adapter.notifyDataSetChanged();
                    llNone.setVisibility(View.GONE);
                    smrRf.setVisibility(View.VISIBLE);
                } else {
                    llNone.setVisibility(View.VISIBLE);
                    smrRf.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String errorMsg) {
                dismissLoading();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    class FolderAdapter extends BaseQuickAdapter<KnowledgeEntity, BaseViewHolderEx> {
        private Activity context;
        private int level;//层级
        private boolean isTask = false;
        private ArrayList<String> nameList;//组织排列
        private String catalogType;//自定义专属字段

        public FolderAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<KnowledgeEntity> data, ArrayList<String> nameList, int level, String catalogType) {
            super(layoutResId, data);
            this.context = context;
            this.nameList = nameList;
            this.level = level;
            this.catalogType = catalogType;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, KnowledgeEntity entity) {
            TextView tvItemTitle = helper.getView(R.id.tvItemTitle);
            TextView tvInit = helper.getView(R.id.tvInit);
            TextView tvInit2 = helper.getView(R.id.tvInit2);
            LinearLayout llAll = helper.getView(R.id.llAll);
            ImageView ivType = helper.getView(R.id.ivType);
            ImageView ivFilesType = helper.getView(R.id.ivFilesType);
            LinearLayout llArrow = helper.getView(R.id.llArrow);
            View line = helper.getView(R.id.line);
            tvItemTitle.setText(entity.getName());
            if (entity.isSelect()) {
                llAll.setBackgroundColor(Color.parseColor("#FFF8EF"));
                line.setVisibility(View.INVISIBLE);
            } else {
                llAll.setBackgroundColor(Color.parseColor("#FFFFFF"));
                line.setVisibility(View.VISIBLE);
            }
            if (entity.getCode().contains("X")) {
                ivType.setVisibility(View.GONE);
                ivFilesType.setVisibility(View.VISIBLE);
                llArrow.setVisibility(View.INVISIBLE);
            } else {
                ivType.setVisibility(View.VISIBLE);
                ivFilesType.setVisibility(View.GONE);
                llArrow.setVisibility(View.VISIBLE);
            }
            if (!Utils.isNull(entity.getName())) {
                if (entity.getName().equals("我的上传")) {
                    tvInit.setVisibility(View.VISIBLE);
                    tvInit.setText("我共上传了" + entity.getFileNum() + "个文件");
                    tvInit2.setVisibility(View.GONE);
                } else {
                    if (Utils.isNull(entity.getFolderNum())) {
                        tvInit.setVisibility(View.GONE);
                    } else {
                        tvInit.setVisibility(View.VISIBLE);
                        tvInit.setText(entity.getFolderNum() + "个文件夹");
                    }
                    if (Utils.isNull(entity.getFileNum())) {
                        tvInit2.setVisibility(View.GONE);
                    } else {
                        tvInit2.setVisibility(View.VISIBLE);
                        tvInit2.setText(entity.getFileNum() + "个文件");
                    }
                }
            }
            helper.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (entity.isSelect()) {
                        folderList.get(helper.getItemPosition()).setSelect(false);
                        folderList.get(helper.getItemPosition()).setHalfSelect(false);
                        folderList.get(helper.getItemPosition()).setUpperHalfSelect(false);
                        adapter.notifyItemChanged(helper.getItemPosition());
                        updateSelect(1, entity);
                    } else {
                        if (entity.isHalfSelect()) {
                            //半选变全选
                            updateSelect(2, entity);
                        } else {
                            //普通选择
                            updateSelect(0, entity);
                        }
                        folderList.get(helper.getItemPosition()).setSelect(true);
                        folderList.get(helper.getItemPosition()).setHalfSelect(false);
                        folderList.get(helper.getItemPosition()).setUpperHalfSelect(false);
                        adapter.notifyItemChanged(helper.getItemPosition());
                    }
                }
            });
            llArrow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (entity.isSelect() || entity.isHalfSelect()) {
                        for (int j = 0; j < MyApp.videoFolderList.size(); j++) {
                            if (entity.getCode().equals(MyApp.videoFolderList.get(j).getCode())) {
                                MyApp.videoFolderList.remove(j);
                                if (j < MyApp.videoFolderList.size()) {
                                    j--;
                                }
                            }
                        }
                    }
                    Intent intent = new Intent(context, VideoNewCatesSecondActivity.class);
                    ArrayList<String> nameNewList = new ArrayList<>();//组织排列
                    nameNewList.addAll(nameList);
                    nameNewList.add(entity.getName());
                    intent.putStringArrayListExtra("nameList", nameNewList);
                    intent.putExtra("level", (level + 1));
                    intent.putExtra("parentCode", entity.getCode());
                    intent.putExtra("videoType", videoType);
                    if (entity.isSelect()) {
                        intent.putExtra("oldDepartmentStatus", true);
                    } else {
                        intent.putExtra("oldDepartmentStatus", false);
                    }
                    intent.putExtra("oldDepartmentEntity", entity);
                    intent.putExtra("levelOne", levelOne);
                    intent.putExtra("hierarchyOrgCode", hierarchyOrgCode + "_" + entity.getCode());
                    context.startActivity(intent);
                }
            });
        }
    }

    class FolderSelectAdapter extends BaseQuickAdapter<KnowledgeEntity, BaseViewHolder> {
        private Context context;

        public FolderSelectAdapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable List<KnowledgeEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder helper, KnowledgeEntity item) {
            LinearLayout llDelete = helper.getView(R.id.llDelete);
            ImageView ivType = helper.getView(R.id.ivType);
            TextView tvTitle = helper.getView(R.id.tvTitle);

            if (item.isSelect()) {
                if (item.getCode().contains("X")) {
                    //文件目录
                    ivType.setBackgroundResource(R.drawable.xwj_list);
                    tvTitle.setText(item.getFolderName() + "的文件(" + item.getFileNum() + ")");
                } else {
                    //文件夹
                    ivType.setBackgroundResource(R.drawable.fxk_qx);
                    tvTitle.setText(item.getName() + "(" + item.getFileNum() + ")");
                }
            } else {
                tvTitle.setText(item.getName() + "(" + item.getHalfSelectNum() + ")");
                ivType.setBackgroundResource(R.drawable.fxk_bx);
            }
            llDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateSelect(1, item);
                }
            });
        }
    }

    private void updateSelect(int type, KnowledgeEntity knowledgeEntity) {
        //0添加 1删除2半选变全选
        KLog.i("------------" + type);
        if (type == 0) {
            knowledgeEntity.setSelect(true);
            MyApp.videoFolderList.add(knowledgeEntity);
        } else if (type == 1) {
            if (knowledgeEntity.isSelect()) {
                //普通清除
                for (int i = 0; i < MyApp.videoFolderList.size(); i++) {
                    if (MyApp.videoFolderList.get(i).getCode().equals(knowledgeEntity.getCode())) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("clear_folder_code", knowledgeEntity.getCode());
                        EventBus.getDefault().post(new MyEvent(map));
                        MyApp.videoFolderList.remove(i);
                        if (i < MyApp.videoFolderList.size()) {
                            i--;
                        }
                    }
                }
            } else {
                //半选清除
                for (int i = 0; i < MyApp.videoFolderList.size(); i++) {
                    if (MyApp.videoFolderList.get(i).getHierarchyOrgCode().contains(knowledgeEntity.getCode()) || MyApp.videoFolderList.get(i).getCode().equals(knowledgeEntity.getCode())) {
                        MyApp.videoFolderList.remove(i);
                        HashMap<String, String> map = new HashMap<>();
                        map.put("clear_folder_code", knowledgeEntity.getCode());
                        EventBus.getDefault().post(new MyEvent(map));
                        if (i < MyApp.videoFolderList.size()) {
                            i--;
                        }
                    }
                }
            }
        } else if (type == 2) {
//            2半选变全选,  把父类是这个code的全部清除
            for (int i = 0; i < MyApp.videoFolderList.size(); i++) {
                if (MyApp.videoFolderList.get(i).getHierarchyOrgCode().contains(knowledgeEntity.getCode()) || MyApp.videoFolderList.get(i).getCode().equals(knowledgeEntity.getCode())) {
                    MyApp.videoFolderList.remove(i);
                    if (i < MyApp.videoFolderList.size()) {
                        i--;
                    }
                }
            }
            knowledgeEntity.setHalfSelect(false);
            knowledgeEntity.setSelect(true);
            MyApp.videoFolderList.add(knowledgeEntity);
        }
        selectDirectoryAllNum = 0;
        for (int i = 0; i < folderList.size(); i++) {
            if (folderList.get(i).isSelect()) {
                selectDirectoryAllNum++;
            }
        }
        changeSelectStatus();
    }

    private void goBack(int oldLevel, int current_Level) {
        HashMap<String, String> map = new HashMap<>();
        int halfSelectNum = 0;
        if (selectDirectoryAllNum == directoryAllNum) {
            //当前目录选中数量和当前当前目录一样，说明全选了
            if (directoryAllNum > 0) {
                for (int i = 0; i < folderList.size(); i++) {
                    for (int j = 0; j < MyApp.videoFolderList.size(); j++) {
                        if (folderList.get(i).getCode().equals(MyApp.videoFolderList.get(j).getCode())) {
                            MyApp.videoFolderList.remove(j);
                            if (j < MyApp.videoFolderList.size()) {
                                j--;
                            }
                        } else if (MyApp.videoFolderList.get(j).getHierarchyOrgCode().contains(folderList.get(i).getCode())) {
                            MyApp.videoFolderList.remove(j);
                            if (j < MyApp.videoFolderList.size()) {
                                j--;
                            }
                        }
                    }
                    ;
                }
                if (oldDepartmentEntity != null) {
                    oldDepartmentEntity.setHalfSelect(false);
                    oldDepartmentEntity.setUpperHalfSelect(false);
                    oldDepartmentEntity.setSelect(true);
                }
                map.put("nextLayerSelectStatus", "true");
                map.put("halfSelect", "false");
            }
        } else {
            if (oldDepartmentEntity != null) {
                oldDepartmentEntity.setHalfSelect(false);
            }
            map.put("nextLayerSelectStatus", "false");
            map.put("halfSelect", "false");

            for (int i = 0; i < folderList.size(); i++) {
                if (folderList.get(i).isHalfSelect() || folderList.get(i).isSelect()) {
                    //下一层级选中的人数，如果nextLayerSelectStatus为ture表示全选此字段无用  false的时候此字段判断是全部没有选中，还是选中一部分
                    map.put("halfSelect", "true");
                    if (oldDepartmentEntity != null) {
                        oldDepartmentEntity.setHalfSelect(true);
                        oldDepartmentEntity.setSelect(false);
                    }
                }
                if (folderList.get(i).isHalfSelect()) {
                    halfSelectNum = halfSelectNum + Utils.changeNumber(folderList.get(i).getHalfSelectNum());
                }
                if (folderList.get(i).isSelect()) {
                    halfSelectNum = halfSelectNum + Utils.changeNumber(folderList.get(i).getFileNum());
                }
            }
            if (halfSelectNum > 0) {
                map.put("nextLayerSelectHalfNum", halfSelectNum + "");
            }
            if (oldDepartmentEntity != null) {
                oldDepartmentEntity.setHalfSelectNum(halfSelectNum + "");
            }
        }
        if (directoryAllNum > 0) {
            if ("true".equals(map.get("nextLayerSelectStatus"))) {
                if (oldDepartmentEntity != null) {
                    MyApp.videoFolderList.add(oldDepartmentEntity);
                }
            } else if ("true".equals(map.get("halfSelect"))) {
                if (oldDepartmentEntity != null) {
                    MyApp.videoFolderList.add(oldDepartmentEntity);
                    for (int i = 0; i < MyApp.videoFolderList.size(); i++) {
                        if (MyApp.videoFolderList.get(i).getHierarchyOrgCode().contains(oldDepartmentEntity.getCode())) {
                            MyApp.videoFolderList.get(i).setUpperHalfSelect(true);
                        }
                    }
                }
            }
            map.put("nextLayerSelectOrgCode", parentCode);
            if (oldLevel > 0 && current_Level > 0) {
                map.put("VideoNewCatesSecondActivity", oldLevel + "");
                map.put("VideoNewCatesSecondActivity_current", (current_Level - 1) + "");
            }
            EventBus.getDefault().post(new MyEvent(map));
            if (level == 2) {
                MyApp.videoFolderMap.clear();
                MyApp.videoFolderMap.putAll(map);
            }
        }
        finish();
    }

    private void dealUpperData(Map<String, String> map) {
        boolean nextLayerSelectStatus = Boolean.parseBoolean(map.get("nextLayerSelectStatus"));
        boolean halfSelect = Boolean.parseBoolean(map.get("halfSelect"));
        String nextLayerSelectOrgCode = map.get("nextLayerSelectOrgCode");
        KLog.i("------------" + nextLayerSelectStatus + "==" + halfSelect);
        selectDirectoryAllNum = 0;
        for (int i = 0; i < folderList.size(); i++) {
            if (folderList.get(i).getCode().equals(nextLayerSelectOrgCode)) {
                folderList.get(i).setSelect(nextLayerSelectStatus);
                if (!nextLayerSelectStatus) {
                    if (halfSelect) {
                        folderList.get(i).setHalfSelect(true);
                        if (map.containsKey("nextLayerSelectHalfNum")) {
                            folderList.get(i).setHalfSelectNum(map.get("nextLayerSelectHalfNum"));
                        }
                    } else {
                        folderList.get(i).setHalfSelect(false);
                    }
                } else {
                    folderList.get(i).setHalfSelect(false);
                }
                KLog.i("------------" + folderList.get(i).isHalfSelect() + "===" + folderList.get(i).isSelect());
                adapter.notifyItemChanged(i);
            }
            if (folderList.get(i).isSelect()) {
                selectDirectoryAllNum++;
            }
        }
        if (map.containsKey("VideoNewCatesSecondActivity") && map.containsKey("VideoNewCatesSecondActivity_current")) {
            int oldLevel = Utils.changeNumber(map.get("VideoNewCatesSecondActivity"));
            int current_Level = Utils.changeNumber(map.get("VideoNewCatesSecondActivity_current"));
            if (level == current_Level && level >= oldLevel) {
                goBack(oldLevel, current_Level);
            }
        }
        if (level == 1) {
            MyApp.videoFolderMap.clear();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getHashMap() != null) {
            Map<String, String> map = messageEvent.getHashMap();
            if (map.containsKey("nextLayerSelectStatus") && map.containsKey(
                    "nextLayerSelectOrgCode")) {
                if (level == 1) {
                    //表明是从第一层目录开始的，直接清除缓存用不上，否则要用 MyApp.videoFolderMap来过度二层到1层的传值
                    MyApp.videoFolderMap.clear();
                }
                dealUpperData(map);
            } else if (map.containsKey("VideoNewCatesSecondActivity") && map.containsKey("VideoNewCatesSecondActivity_current")) {
                int oldLevel = Utils.changeNumber(map.get("VideoNewCatesSecondActivity"));
                int current_Level = Utils.changeNumber(map.get("VideoNewCatesSecondActivity_current"));
                if (level == current_Level && level > oldLevel) {
                    goBack(oldLevel, current_Level);
                }
            } else if (map.containsKey("clear_folder_code")) {
                String code = map.get("clear_folder_code");
                //刷新当前目录列表
                for (int i = 0; i < folderList.size(); i++) {
                    if (folderList.get(i).getCode().equals(code)) {
                        folderList.get(i).setHalfSelect(false);
                        folderList.get(i).setSelect(false);
                        adapter.notifyItemChanged(i);
                    }
                }
            }
        } else {
            String msg = messageEvent.getMessage();
            if (Utils.isNull(msg)) {
                return;
            }
            if ("CloseVideoNewCates".equals(msg)) {
                //刷新最近列表
                if (level == 2) {
                    if (VideoNewCatesSecondActivity.videoNewCatesSecondActivity != null) {
                        VideoNewCatesSecondActivity.videoNewCatesSecondActivity.finish();
                    }
                }
                finish();
            }
        }
    }

    private void changeSelectStatus() {
        //更新底部弹窗区域数据
        if (MyApp.videoFolderMap.containsKey("nextLayerSelectStatus") && MyApp.videoFolderMap.containsKey(
                "nextLayerSelectOrgCode")) {
            dealUpperData(MyApp.videoFolderMap);
        }
        selectAllNum = 0;
        folderSelectList.clear();
        for (int i = 0; i < MyApp.videoFolderList.size(); i++) {
            if (MyApp.videoFolderList.get(i).isSelect()) {
                selectAllNum = selectAllNum + Utils.changeNumber(MyApp.videoFolderList.get(i).getFileNum());
            }

            if (!MyApp.videoFolderList.get(i).isUpperHalfSelect()) {
                //上层是半选的当前层级不显示
                if (MyApp.videoFolderList.get(i).isSelect() || MyApp.videoFolderList.get(i).isHalfSelect()) {
                    folderSelectList.add(MyApp.videoFolderList.get(i));
                }
            }
            KLog.i("------------" + MyApp.videoFolderList.get(i).getHierarchyOrgCode() + "==" + MyApp.videoFolderList.get(i).getName() + "==" + MyApp.videoFolderList.get(i).isSelect() + "==" + MyApp.videoFolderList.get(i).isHalfSelect() + "==" + MyApp.videoFolderList.get(i).isUpperHalfSelect() + "==" + MyApp.videoFolderList.get(i).getHalfSelectNum());
        }
        folderSelectAdapter.notifyDataSetChanged();
        if (videoType.equals("3")) {
            //发布视频是单选，底部弹窗么有
            llBottomPopup.setVisibility(View.GONE);
            if (MyApp.videoFolderList.size() > 0) {
                tvBottomSure.setText("上传到" + MyApp.videoFolderList.get(0).getName());
            } else {
                tvBottomSure.setText("确定");
            }
        } else {
            if (MyApp.videoFolderList.size() > 0) {
                llBottomPopup.setVisibility(View.VISIBLE);
            } else {
                llBottomPopup.setVisibility(View.GONE);
            }
            if (folderSelectList.size() > 0) {
                //数据>0 直接展开
                isOpen = true;
                ivBottomArrow.setBackgroundResource(R.drawable.arrow_zk);
                folderSelectLayout.setVisibility(View.VISIBLE);
            } else {
                //没有数据默认隐藏
                isOpen = false;
                ivBottomArrow.setBackgroundResource(R.drawable.arrow_sq);
                folderSelectLayout.setVisibility(View.GONE);
            }
        }

        tvBottom.setText(selectAllNum + "个文件");

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //比onMoonEvent 慢
        changeSelectStatus();
    }

    class TitleAdapter extends BaseQuickAdapter<String, BaseViewHolderEx> {
        private Activity context;

        public TitleAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<String> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, String entity) {
            TextView tvItemTitle = helper.getView(R.id.tvTitle);
            ImageView ivArrow = helper.getView(R.id.ivArrow);
            tvItemTitle.setText(entity);
            if (helper.getItemPosition() > 0) {
                ivArrow.setVisibility(View.VISIBLE);
            } else {
                ivArrow.setVisibility(View.GONE);
            }
            if (helper.getItemPosition() == nameList.size() - 1) {
                tvItemTitle.setTextColor(Color.parseColor("#333333"));
            } else {
                tvItemTitle.setTextColor(Color.parseColor("#8a8a8a"));
            }
            helper.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Urls.antiShake.check()) {
                        return;
                    }
                    HashMap<String, String> map = new HashMap<>();
                    if (helper.getItemPosition() == 0) {
                        if (levelOne != null) {
                            Intent intent = null;
                            intent = new Intent(VideoNewCatesSecondActivity.this, VideoNewCatesSecondActivity.class);
                            intent.putExtra("hierarchyOrgCode", levelOne.getCode());
                            intent.putExtra("oldDepartmentEntity", levelOne);
                            intent.putExtra("parentCode", levelOne.getCode());
                            intent.putExtra("parentName", levelOne.getName());
                            intent.putExtra("parentFileNum", levelOne.getFileNum());
                            intent.putExtra("videoType", videoType);
                            intent.putExtra("levelOne", levelOne);
                            intent.putExtra("level", 1);
                            ArrayList<String> nameList = new ArrayList<>();//组织排列
                            nameList.add(levelOne.getName());
                            intent.putStringArrayListExtra("nameList", nameList);
                            startActivity(intent);
                            MyApp.videoFolderMap.clear();
                        }
                        map.put("VideoNewCatesSecondActivity", "1");
                        map.put("VideoNewCatesSecondActivity_current", level + "");
                        EventBus.getDefault().post(new MyEvent(map));
                    } else {
                        map.put("VideoNewCatesSecondActivity", (helper.getItemPosition() + 1) + "");
                        map.put("VideoNewCatesSecondActivity_current", level + "");
                        EventBus.getDefault().post(new MyEvent(map));
                    }
                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        if (level > 1) {
            goBack(0, 0);
        } else {
            //第一级目录不处理
            finish();
        }

    }

    @OnClick({R.id.title_back, R.id.llBottom, R.id.llBottomArrow})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                if (level > 1) {
                    goBack(0, 0);
                } else {
                    //第一级目录不处理
                    finish();
                }
                break;
            case R.id.llBottom:
                if (MyApp.videoFolderList.size() == 0) {
                    MyToast.show("最少选择一个");
                } else {
                    if (selectDirectoryAllNum == directoryAllNum && level > 1) {
                        //全选当前目录
                        if (oldDepartmentEntity != null) {
                            for (int i = 0; i < folderList.size(); i++) {
                                for (int j = 0; j < MyApp.videoFolderList.size(); j++) {
                                    if (folderList.get(i).getCode().equals(MyApp.videoFolderList.get(j).getCode())) {
                                        MyApp.videoFolderList.remove(j);
                                        if (j < MyApp.videoFolderList.size()) {
                                            j--;
                                        }
                                    } else if (MyApp.videoFolderList.get(j).getHierarchyOrgCode().contains(folderList.get(i).getCode())) {
                                        MyApp.videoFolderList.remove(j);
                                        if (j < MyApp.videoFolderList.size()) {
                                            j--;
                                        }
                                    }
                                }
                                ;
                            }
                            oldDepartmentEntity.setHalfSelect(false);
                            oldDepartmentEntity.setSelect(true);
                            MyApp.videoFolderList.add(oldDepartmentEntity);
                        }
                    }
                    String codes = "", videoNames = "";
                    for (int i = 0; i < MyApp.videoFolderList.size(); i++) {
                        if (MyApp.videoFolderList.get(i).isSelect()) {
                            if (Utils.isNull(codes)) {
                                codes = codes + MyApp.videoFolderList.get(i).getCode();
                                videoNames = videoNames + MyApp.videoFolderList.get(i).getName();
                            } else {
                                codes = codes + "," + MyApp.videoFolderList.get(i).getCode();
                                videoNames = videoNames + "," + MyApp.videoFolderList.get(i).getName();
                            }
                        }
                    }
                    if ("1".equals(videoType)) {
                        //小视频
                        Intent intent = new Intent(this, VideoCatesPlayActivity.class);
                        intent.putExtra("videoCate", codes);
                        intent.putExtra("watermark", "4");
                        intent.putExtra("type", 1);
                        startActivity(intent);
                    } else if ("2".equals(videoType)) {
                        //长视频
                        Intent intent = new Intent(this, LongVediosActivity.class);
                        intent.putExtra("videoCate", codes);
                        startActivity(intent);
                    } else if ("5".equals(videoType) || "6".equals(videoType)) {
                        //图库目录选择
                        Map<String, String> map = new HashMap<>();
                        map.put("videoCate" + videoType, codes);
                        map.put("videoNames", videoNames);
                        EventBus.getDefault().post(new MyEvent(map));
                    }
                    EventBus.getDefault().post(new MyEvent("CloseVideoNewCates"));
                }
                break;
            case R.id.llBottomArrow:
                if (isOpen) {
                    isOpen = false;
                    ivBottomArrow.setBackgroundResource(R.drawable.arrow_sq);
                    folderSelectLayout.setVisibility(View.GONE);
                } else {
                    isOpen = true;
                    ivBottomArrow.setBackgroundResource(R.drawable.arrow_zk);
                    folderSelectLayout.setVisibility(View.VISIBLE);
                }
                break;
        }
    }


}
